#!/bin/bash

# --------------------------
# 导出数据中台数据
# --------------------------

export() {
  mysqldump -P3307 -u$MYSQL_USER -p$MYSQL_PASSWORD --no-data \
  --default-character-set=utf8 --databases data_platform > /tmp/mysql-ddl.sql

  mysqldump -P3307 -u$MYSQL_USER -p$MYSQL_PASSWORD \
  --no-create-info --default-character-set=utf8 --databases data_platform \
  --ignore-table=data_platform.analysis_statistical_task_compensate \
  --ignore-table=data_platform.analysis_table_info \
  --ignore-table=data_platform.analysis_task_execute_log \
  --ignore-table=data_platform.analysis_record_log \
  --ignore-table=data_platform.task_schedule_record \
  --ignore-table=data_platform.analysis_detail_rule \
  --ignore-table=data_platform.analysis_basic_quota_table \
  --ignore-table=data_platform.etl_job_detail \
  --ignore-table=data_platform.heartbeat_except \
  > /tmp/mysql-dml.sql
}

BUSINESS_TYPE='yundi'

MYSQL_DIR=/usr/local/mysql-8.0.34-macos13-arm64/bin
current_date=$(date +%Y%m%d)
EXPORT_PATH_DDL="/tmp/${BUSINESS_TYPE}-${current_date}-ddl.sql"
EXPORT_PATH_DML="/tmp/${BUSINESS_TYPE}-${current_date}-dml.sql"

# 云堤环境
#MYSQL_HOST='10.16.129.201'
#MYSQL_PORT='3306'
#MYSQL_USERNAME='root'
#MYSQL_PASSWORD='mssp@ct2'
#DATABASE='data_platform'

# mss-测试环境
MYSQL_HOST='10.50.26.148'
MYSQL_PORT='3307'
MYSQL_USERNAME='mysql'
MYSQL_PASSWORD='OnEPXyDgqR'
DATABASE='data_platform'

echo "开始导出 $BUSINESS_TYPE mysql 数据"

echo "导出 mysql ddl 到文件：$EXPORT_PATH_DDL"
${MYSQL_DIR}/mysqldump -h$MYSQL_HOST -P$MYSQL_PORT -u$MYSQL_USERNAME -p$MYSQL_PASSWORD \
  --no-data --default-character-set=utf8 --databases data_platform \
  > $EXPORT_PATH_DDL

echo "导出 mysql dml 到文件：$EXPORT_PATH_DML"
${MYSQL_DIR}/mysqldump -h$MYSQL_HOST -P$MYSQL_PORT -u$MYSQL_USERNAME -p$MYSQL_PASSWORD \
--no-create-info --default-character-set=utf8 --databases data_platform \
--ignore-table=data_platform.analysis_statistical_task_compensate \
--ignore-table=data_platform.analysis_table_info \
--ignore-table=data_platform.analysis_task_execute_log \
--ignore-table=data_platform.analysis_record_log \
--ignore-table=data_platform.task_schedule_record \
--ignore-table=data_platform.analysis_detail_rule \
--ignore-table=data_platform.analysis_basic_quota_table \
--ignore-table=data_platform.etl_job_detail \
--ignore-table=data_platform.heartbeat_except \
> $EXPORT_PATH_DML

echo "导出完成"

docker exec -it mysql-master-server mysqldump -hmysql.ahcloud-private.com -P3307 -u$MYSQL_USER -p$MYSQL_PASSWORD \
                        --no-data --default-character-set=utf8 --databases data_platform /tmp/ddl.sql

docker exec -it mysql-master-server mysqldump -hmysql.ahcloud-private.com -P3307 -u$MYSQL_USER -p \
                        --no-data --default-character-set=utf8 --databases data_platform /tmp/ddl.sql

docker exec -it mysql-master-server mysqldump -hmysql.ahcloud-private.com -P3307 -umysql -p$MYSQL_PASSWORD --no-data --default-character-set=utf8 --databases data_platform /tmp/ddl.sql

docker exec -it mysql-master-server mysqldump -hmysql.ahcloud-private.com -P3307 -umysql -p$MYSQL_PASSWORD \
--no-create-info --default-character-set=utf8 --databases data_platform \
--ignore-table=data_platform.analysis_statistical_task_compensate \
--ignore-table=data_platform.analysis_table_info \
--ignore-table=data_platform.analysis_task_execute_log \
--ignore-table=data_platform.analysis_record_log \
--ignore-table=data_platform.task_schedule_record \
--ignore-table=data_platform.analysis_detail_rule \
--ignore-table=data_platform.analysis_basic_quota_table \
--ignore-table=data_platform.etl_job_detail \
--ignore-table=data_platform.heartbeat_except \
/tmp/dml.sql

docker cp mysql-master-server:/tmp/ddl.sql /tmp/

OnEPXyDgqR